<template>
  <section class="plan-container tapDemand-panel">
    <div class="options-tool" v-if="row.isView">
      <el-button @click="printPDF" type="success">下载</el-button>
      <el-button @click="handleBack">返回</el-button>
    </div>
    <div class="panel" ref="pdfContent">
      <div class="plan-title">四川省食品药品审查评价及安全监测中心</div>
      <Table :columns="columns" :tableData="tableData" :operation="operation" />

      <Process
        :processData="processData"
        v-if="row.isView && processData.length"
      />
      <div class="dialog-footer" v-if="!row.isAuth && !row.isView">
        <el-button
          v-for="(item, index) in formBtns"
          :key="index"
          @click="item.click"
          :type="item.type"
          >{{ item.label }}</el-button
        >
      </div>
      <div class="form-panel collect" v-if="row.isAuth">
        <Form
          :formData="formData"
          :formBtns="formBtns"
          ref="form"
          @handleSubmit="handleSubmit"
        />
      </div>
    </div>
    <Audit
      @onBack="
        () => {
          this.isAudit = false;
        }
      "
      @onSuccess="onSuccess"
      from="plan"
      :row="row"
      v-if="isAudit"
    />
    <AuditLeader
      @onBack="
        () => {
          this.isLeaderAudit = false;
        }
      "
      @onLeaderSuccess="onLeaderSuccess"
      v-if="isLeaderAudit"
    />
    <input
      type="file"
      id="fileInput"
      style="display: none"
      @change="handleFileChange"
    />

    <el-dialog
      title="查看文件"
      :visible.sync="open"
      v-if="open"
      width="800px"
      append-to-body
      @close="
        () => {
          this.open = false;
        }
      "
    >
      <UploadFile :uploadData="uploadData" />
    </el-dialog>
  </section>
</template>

<script>
import Table from "@/components/table/index.vue";
import Form from "@/components/form/index.vue";
import Process from "../audit/process.vue";
import Audit from "../audit/index.vue";
import AuditLeader from "../audit/auditLeader.vue";
import UploadFile from "@/components/uploadFile/index.vue";
import {
  vendorScheduleGetInfo,
  vendorScheduleResponsibilityAudit,
  vendorScheduleUpdate,
  vendorScheduleOperatorAudit,
} from "@/api/collect/plan/index";
import { uploads } from "@/api/common/index";
export default {
  components: {
    Table,
    Form,
    Process,
    Audit,
    AuditLeader,
    UploadFile,
  },
  props: {
    row: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      id: "",
      detail: {},
      isView: false,
      isAudit: false,
      isLeaderAudit: false,
      uploadData: { list: [], disabled: true },
      open: false,
      rowKey: 0,
      processData: [],
      columns: [
        {
          prop: "yearGroup",
          label: "年度组号",
          hiddenTooltip: true,
          width: 100,
          fixed: true,
        },
        {
          prop: "followNo",
          label: "企业流水号/家次",
          width: 100,
          hiddenTooltip: true,
          scope: true,
          disabled: this.row.isView || this.row.isAuth || this.row.status == 4,
          input: (data, obj, key) => {},
        },
        {
          prop: "checkDate",
          label: "检查时间",
          width: 200,
          formatter: (row) => {
            return row.checkStartDate
              ? row.checkStartDate + "至" + row.checkEndDate
              : "";
          },
        },
        {
          prop: "applyReason",
          label: "检查事项",
          width: 160,
          hiddenTooltip: true,
        },
        {
          prop: "vendorName",
          label: "检查对象（企业全称）",
          width: 200,
          hiddenTooltip: true,
        },
        {
          prop: "checkAddress",
          label: "检查/核查地点",
          width: 160,
          hiddenTooltip: true,
        },
        {
          prop: "checkRange",
          label: "检查范围",
          width: 160,
          hiddenTooltip: true,
        },
        {
          prop: "followMember",
          label: "跟随学习",
          scope: true,
          type: "textarea",
          width: 160,
          disabled: this.row.isView || this.row.isAuth || this.row.status == 4,
          input: (data, obj, key) => {},
        },
        {
          prop: "checkLeaderName",
          label: "检查组长",
          scope: true,
          type: "textarea",
          disabled: this.row.isView || this.row.isAuth || this.row.status == 4,
          input: (data, obj, key) => {},
        },
        {
          prop: "checkMember1",
          label: "组员",
          scope: true,
          type: "textarea",
          disabled: this.row.isView || this.row.isAuth || this.row.status == 4,
          input: (data, obj, key) => {},
        },
      ],
      operation: [
        //列表操作栏
        {
          width: 160,
          fixed: true,
          label: "上传文件",
          onclick: (key, row) => {
            this.rowKey = key;
            document.getElementById("fileInput").click();
          },
        },
        {
          label: "查看文件",
          onclick: (key, row) => {
            this.open = true;
            this.uploadData.list = row.fileList || [];
          },
        },
      ],
      tableData: [], //列表数据
      formData: [
        {
          prop: "status",
          type: "select",
          options: [
            {
              label: "通过",
              value: "1",
            },
            {
              label: "拒绝",
              value: "0",
            },
          ],
          label: "审批结果",
          value: "1",
        },
        {
          prop: "remark",
          type: "textarea",
          label: "审批意见",
          rows: 4,
        },
      ],
      formAudit: {},
      formBtns: [
        //表单按钮
        {
          type: "primary",
          label: "提交审核",
          click: () => {
            this.isAudit = true;
          },
        },
        {
          type: "success",
          label: "保存",
          click: () => {
            let tableData = [...this.tableData];
            tableData.forEach((item) => {
              item.checkLeader = item.checkLeaderName;
              if (item.checkData && item.checkData.length) {
                item.checkStartDate = item.checkData[0];
                item.checkEndDate = item.checkData[1];
                delete item.checkData;
              }
            });
            let params = {
              batchNo: this.row.batchNo,
              checkDataList: tableData,
            };
            vendorScheduleUpdate(params).then((res) => {
              this.Message("保存成功");
              this.$emit("onSuccess");
            });
          },
        },
        {
          label: "返回",
          click: () => {
            this.$emit("onBack");
          },
        },
      ],
    };
  },
  created() {
    if (this.$route.query.isView || this.row.isView) {
      this.formBtns = [];
    }
    if (this.row.isAuth) {
      this.formBtns = [
        {
          type: "primary",
          label: "确认",
          click: () => {
            let tableData = [...this.tableData];
            tableData.forEach((item) => {
              item.checkLeader = item.checkLeaderName;
              if (item.checkData && item.checkData.length) {
                item.checkStartDate = item.checkData[0];
                item.checkEndDate = item.checkData[1];
                delete item.checkData;
              }
            });
            let params = {
              ...this.$refs.form.datas,
              batchNo: this.row.batchNo,
              checkDataList: tableData,
            };
            vendorScheduleResponsibilityAudit(params).then((res) => {
              this.Message("审核成功");
              this.$emit("onSuccess");
            });
          },
        },
        {
          label: "返回",
          click: () => {
            this.handleBack();
          },
        },
      ];
    }
  },
  mounted() {
    if (this.row.batchNo) {
      this.getLoad();
    }
  },
  methods: {
    //获取数据
    getLoad() {
      vendorScheduleGetInfo({ batchNo: this.row.batchNo }).then((res) => {
        this.detail = res.data;
        let tableData = res.data.vendorInfoList || [];
        this.processData = res.data.auditList || [];
        tableData.forEach((item) => {
          item.fileList = item.fileList || [];
          if (item.checkMember) {
            let checkMember = item.checkMember.split(",");
            checkMember.forEach((v, index) => {
              let key = "checkMember" + (index + 1);
              this.$set(item, key, v);
            });
          }
        });

        let num = tableData[0].checkNumber;
        if (num - 1 > 1) {
          for (let i = 0; i < num - 2; i++) {
            this.columns.push({
              prop: "checkMember" + (i + 2),
              label: "组员",
              scope: true,
              disabled:
                this.row.isView || this.row.isAuth || this.row.status == 4,
              type: "textarea",
              input: (data, obj, key) => {},
            });
          }
        }
        this.tableData = tableData;
      });
    },
    //科室负责人审核
    handleSubmit(data) {
      this.formAudit = data;
      this.isLeaderAudit = true;
    },
    //经办人审核回调
    onSuccess(data) {
      let array = data.nextAudit.split("/");
      let tableData = [...this.tableData];
      tableData.forEach((item) => {
        item.checkLeader = item.checkLeaderName;
        if (item.checkData && item.checkData.length) {
          item.checkStartDate = item.checkData[0];
          item.checkEndDate = item.checkData[1];
          delete item.checkData;
        }
      });
      let params = {
        nextAuditId: array[0],
        nextAuditName: array[1],
        batchNo: this.row.batchNo,
        checkDataList: tableData,
      };
      //是否科室负责人审核
      if (data.flag) {
        params.flag = false;
      }
      if (this.row.status == 4) {
        params.status = 1;
        vendorScheduleResponsibilityAudit(params).then((res) => {
          this.Message("审核成功");
          this.$emit("onSuccess");
        });
        return;
      }
      vendorScheduleOperatorAudit(params).then((res) => {
        this.Message("审核成功");
        this.$emit("onSuccess");
      });
    },
    handleFileChange(event) {
      const file = event.target.files[0];
      if (file) {
        let formFile = new FormData();
        formFile.append("files", file);
        uploads(formFile).then((response) => {
          this.tableData[this.rowKey].fileList = [
            ...this.tableData[this.rowKey].fileList,
            ...response.data,
          ];
        });
        console.log("this.tableData", this.tableData);
      }
    },
    //科室负责人审核回调
    onLeaderSuccess() {
      vendorScheduleResponsibilityAudit({
        batchNo: this.row.batchNo,
        ...this.formAudit,
      }).then((res) => {
        this.Message("审核成功");
        this.$emit("onSuccess");
      });
    },
    //返回
    handleBack() {
      this.$emit("onBack");
    },
    //下载pdf
    printPDF() {
      this.PrintPDF("安排计划表表", this);
    },
  },
};
</script>

<style lang="scss">
@import "../../../assets/styles/collect.scss";
.plan-container {
  padding: 30px;
}
.options-tool {
  font-size: 16px;
  text-align: right;
}
.plan-title {
  padding: 20px;
  font-size: 22px;
  text-align: center;
}
.plan-panel {
  margin: 20px 0 0;
}
</style>
